/**
 * 
 */
package com.edocbase.biz.nodes;

import org.apache.commons.lang.StringUtils;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.Image;

import com.edocbase.biz.diary.entities.IDiary;
import com.edocbase.desktop.maildriver.DesktopApplication;
import com.edocbase.desktop.plugins.mdiary.MDiaryActionConstants;

/**
 * @author X61
 * 
 */
public class DiaryLabelProvider extends LabelProvider {

	@Override
	public Image getImage(Object element) {
		if (element instanceof DiaryTreeNode) {
			DiaryTreeNode diaryNode = (DiaryTreeNode) element;
			if (diaryNode.getType() == MDiaryActionConstants.DIARY_NODE_CATALOG_TYPE) {
				return DesktopApplication.getImage("/images/file/diary_path.gif");
			} else if (diaryNode.getType() == MDiaryActionConstants.DIARY_NODE_TYPE) {
				return DesktopApplication.getImage("/images/file/diary_file.gif");
			}
		}
		return super.getImage(element);
	}

	@Override
	public String getText(Object element) {
		if (element instanceof DiaryTreeNode) {
			DiaryTreeNode diaryNode = (DiaryTreeNode) element;
			if (diaryNode.getType() == MDiaryActionConstants.DIARY_NODE_CATALOG_TYPE) {
				IDiary diary = diaryNode.getDiary();
				if (diary == null){
					return "";
				}
				if (diary.getCatalogPath() != null) {
					return diary.getCatalogPath();
				} else {
					return "UNKNOW...";
				}
			} else if (diaryNode.getType() == MDiaryActionConstants.DIARY_NODE_TYPE) {
				IDiary diary = diaryNode.getDiary();
				if (diary == null){
					return "";
				}
				if (diary.getTitle() != null) {
					String title = diary.getTitle();
					title = StringUtils.replace(title, "\r\n", "");
					title = StringUtils.replace(title, "\r", "");
					title = StringUtils.replace(title, "\n", "");
					title = StringUtils.replace(title, "\r\n", "");
					return title;
				} else {
					return "UNKNOW...TITLE";
				}
			}
		}
		return super.getText(element);
	}

}
